package com.lfg.leetcode.leetcode0;


import java.util.HashMap;
import java.util.Map;

public class No3LengthOfLongestSubstring {


    public static int lengthOfLongestSubstring(String content) {
        if (null == content || "".equals(content)) {
            return 0;
        }
        int ret = 0;
        int length = content.length();
        int left = 0;
        Integer[] data = new Integer[128];
        for (int i = 0; i < length; i++) {
            Character c = content.charAt(i);
            Integer index = data[c];
            if (index != null && index >= left) {
                left = index + 1;
            }
            data[c] = i;
            int l = i - left + 1;
            if (l > ret) {
                ret = l;
            }
        }
        return ret;
    }


    public static int lengthOfLongestSubstring2(String content) {
        if (null == content || "".equals(content)) {
            return 0;
        }
        int ret = 0;
        int length = content.length();
        for (int i = 0; i < length; i++) {
            int n = i + 1;
            Map<Character, Integer> data = new HashMap<Character, Integer>();
            data.put(content.charAt(i), i);
            while (n < length && !data.containsKey(content.charAt(n))) {
                data.put(content.charAt(n), n);
                n++;
            }
            int l = n - i;
            if (l > ret) {
                ret = l;
            }
            i = n;
        }
        return ret;
    }


    public static void main(String[] args) {

//        System.out.println(lengthOfLongestSubstring("au"));
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }
}
